package com.online.controller;

import com.online.entity.Blank;
import com.online.entity.Exam;
import com.online.entity.Option;
import com.online.entity.StudentAnswer;
import com.online.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/studentAnswer")
public class StudentAnswerController {

    @Autowired
    StudentAnswerService studentAnswerService;

    @Autowired
    ExamService examService;

    @Autowired
    PaperService paperService;

    @Autowired
    OptionService optionService;

    @Autowired
    BlankService blankService;

    /**
     * 学生提交一份试卷答案
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/addAAnswer",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String addAStudentAnswer(@RequestParam("examId") String examId, HttpServletRequest httpServletRequest) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        String studentNumber =(String) subject.getPrincipal();

        //获取当前考试的id
        int examNumber = Integer.parseInt(examId);

        //查询当前考试的基本信息
        Exam exam = new Exam();
        exam = examService.showAExam(examNumber);

        //获取选择题和填空题数量
        int optionsNumber = exam.getOptionsNumber();
        int blanksNumber = exam.getBlanksNumber();

        //存放选择题和填空题的答案
        List<String> optionsList = new ArrayList<>();
        List<String> blanksList = new ArrayList<>();

        for(int i = 1; i <= optionsNumber; i++) {
            String optionAnswer = httpServletRequest.getParameter(String.valueOf(i));
            optionsList.add(optionAnswer);
        }

        for(int i = 1; i <= blanksNumber; i++) {
            String blankAnswer = httpServletRequest.getParameter("blanksId" + String.valueOf(i));
            blanksList.add(blankAnswer);
        }

        for(int i = 0; i < optionsList.size(); i++) {
            System.out.println("选择题:" + (i+1) + ":" + optionsList.get(i));
        }

        for(int i = 0; i < blanksList.size(); i++) {
            System.out.println("填空题:" + (i+1) + ":" + blanksList.get(i));
        }

        int result = 0;
        StudentAnswer studentAnswers = new StudentAnswer();

        //插入学生的选择题答案
        for(int i = 0; i < optionsList.size(); i++) {
            studentAnswers.setExamId(examNumber);
            studentAnswers.setStudentNumber(studentNumber);
            studentAnswers.setQuestionType(0);
            studentAnswers.setQuestionENumber(i+1);
            studentAnswers.setStudentAnswer(optionsList.get(i));
            result = studentAnswerService.insertAStudentAnswer(studentAnswers);
        }

        //插入填空题
        for(int i = 0; i < blanksList.size(); i++) {
            studentAnswers.setExamId(examNumber);
            studentAnswers.setStudentNumber(studentNumber);
            studentAnswers.setQuestionType(1);
            studentAnswers.setQuestionENumber(i+1);
            studentAnswers.setStudentAnswer(blanksList.get(i));
            result = studentAnswerService.insertAStudentAnswer(studentAnswers);
        }

        if(result == 1) {
            httpServletRequest.setAttribute("result","答卷提交成功");
            return "student/submit_paper_result";
        }else {
            httpServletRequest.setAttribute("result","答卷提交失败");
            return "student/submit_paper_result";
        }
    }

    /**
     * 返回当前用户的答卷列表
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/answerPaperList",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String showAnswerPaperList(HttpServletRequest httpServletRequest) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        String studentNumber =(String) subject.getPrincipal();

        //查询当前登录用户的答卷id
        List<Integer> answerPaperList = studentAnswerService.showStudentAnswerExamId(studentNumber);
        if(answerPaperList == null) {
            httpServletRequest.setAttribute("result","你暂未提交任何答卷!");
        }

        List<Exam> examList = new ArrayList<>();
        //根据答卷id查询具体内容
        for(int i = 0; i<answerPaperList.size(); i++) {
            Exam exam = examService.showAExam(answerPaperList.get(i));
            examList.add(exam);
        }
        httpServletRequest.setAttribute("examList",examList);
        httpServletRequest.setAttribute("studentNumber",studentNumber);
        return "student/student_answer_paperList";
    }


    /**
     * 查询学生一份答卷的具体信息
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/answerPaperDetail",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String showAAnswerPaperDetail(HttpServletRequest httpServletRequest) {

        Integer examId = Integer.parseInt(httpServletRequest.getParameter("examId")) ;
        String studentNumber = httpServletRequest.getParameter("studentNumber");

        //查询试卷中的题目的题库编号
        List<Integer> optionIdList = paperService.showAllQuestionId(examId, 0);
        List<Integer> blankIdList = paperService.showAllQuestionId(examId, 1);
        Option option = new Option();
        Blank blank = new Blank();

        List<Option> optionList = new ArrayList<>();
        List<Blank> blankList = new ArrayList<>();

        //查询试卷中每一道题的详细情况
        for (int i = 0; i < optionIdList.size(); i++) {
            option = optionService.showOptionById(optionIdList.get(i));
            optionList.add(option);
        }

        for (int i = 0; i < blankIdList.size(); i++) {
            blank = blankService.showBlankById(blankIdList.get(i));
            blankList.add(blank);
        }

        //查询对应的答案
        List<StudentAnswer> optionAnswerList = studentAnswerService.showStudentOptionAnswer(studentNumber,examId);
        List<StudentAnswer> blankAnswerList = studentAnswerService.showStudentBlankAnswer(studentNumber,examId);

        httpServletRequest.setAttribute("blankAnswerList",blankAnswerList);
        httpServletRequest.setAttribute("optionAnswerList",optionAnswerList);

        httpServletRequest.setAttribute("optionList", optionList);
        httpServletRequest.setAttribute("blankList",blankList);

        return "student/answer_paper_detail";
    }
}
